package com.qtenv.datasync.base;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.qtenv.datasync.constant.AppConstants;
import com.qtenv.datasync.service.entity.Terminals_bf;
import com.qtenv.datasync.service.service.TerminalsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public abstract class ArchiveSyncer {
    @Autowired
    private TerminalsService terminalsService;

    /**
     * 厂家名称
     */
    private String supplier;

    public ArchiveSyncer() {
    }

    public ArchiveSyncer(String supplier) {
        this.supplier = supplier;
    }

    public String getSupplier() {
        return supplier;
    }

    /**
     * 获取厂家档案列表
     *
     * @return
     */
    public abstract List<Terminals_bf> getTerminals();

    /**
     * 将厂家档案数据同步到数据库
     */
    public void syncTerminals() {
        List<Terminals_bf> terminalsList = this.getTerminals();
        if (CollectionUtil.isNotEmpty(terminalsList)) {
            System.out.println("============" + JSONUtil.toJsonPrettyStr(terminalsList));
            System.out.println(terminalsService);
            List<Terminals_bf> records = terminalsService.findMasTerminalsByTenantAndSupplier(AppConstants.APP_TENANT_ID, terminalsList.get(0).getSupplierID());
            terminalsList.removeIf(item -> records.stream().filter(record -> item.getTerminalID().equals(record.getTerminalID())).count() > 0);
            int size = terminalsList.size();
            if (size > 0) {
                System.out.println("新增档案数量：" + size);
                terminalsService.saveToMiddle(terminalsList);
            }
        } else {
            System.out.println("厂家：【" + this.supplier + "】暂无档案数据需要同步！");
        }
    }
}
